<?php 
error_reporting(E_ERROR | E_WARNING | E_PARSE);
require 'Spreadsheet/Excel/Writer.php';
include('../config.php');
if(isset($_GET['filename']))
{
	$filename = $_GET['filename']; //chitiet/sotien/huy
	$marketing = $_GET['marketing']; //chitiet/sotien/huy
	$tungay_dmY = $_GET['tungay']; //24/11/2011
	$tungay_a = explode('/', $tungay_dmY);
	$tungay_d = $tungay_a[0];
	$tungay_m = $tungay_a[1];
	$tungay_Y = $tungay_a[2];
	$tungay = strtotime($tungay_Y.'/'.$tungay_m.'/'.$tungay_d) - 1; //echo $tungay."-";
	$denngay_dmY = $_GET['denngay']; //24/11/2011
	$denngay_a = explode('/', $denngay_dmY);
	$denngay_d = $denngay_a[0];
	$denngay_m = $denngay_a[1];
	$denngay_Y = $denngay_a[2];
	$denngay = strtotime($denngay_Y.'/'.$denngay_m.'/'.$denngay_d) + 1; //echo $denngay;
	$export_file = $filename."_".$tungay_d."-".$tungay_m."-".$tungay_Y."_".$denngay_d."-".$denngay_m."-".$denngay_Y.".xls"; 
	if($marketing!=''){$export_file = '('.$marketing.')'.$export_file;}
	$select = mysql_query("SELECT * FROM `company` WHERE `c_date` > '$tungay' AND `c_date` < '$denngay' AND `c_marketing` = '$marketing'") or die ("21");
	$tktotalcty = mysql_num_rows($select);
	$thanhtien = '';
	$vatamount = '';
	$tongtien = '';
	if($filename == 'chitiet')
	{
		$workbook = new Spreadsheet_Excel_Writer();
		$workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet('Chi Tiết');
		$worksheet1->freezePanes(array(1, 0));
		$worksheet1->setInputEncoding('UTF-8');
		$worksheet1->setColumn(0,1,4);
		$worksheet1->setColumn(2,2,40);
		$worksheet1->setColumn(3,3,14);
		$worksheet1->setColumn(4,4,40);
		$worksheet1->setColumn(5,5,20);
		$worksheet1->setColumn(6,6,15);
		$worksheet1->setColumn(7,7,30);
		$worksheet1->setColumn(8,8,10);
		$worksheet1->setColumn(9,9,10);
		$worksheet1->setColumn(10,10,25);
		$worksheet1->setColumn(11,11,14);
		$worksheet1->setColumn(12,12,8);
		$worksheet1->setColumn(13,13,8);
		$worksheet1->setColumn(14,14,10);
		$worksheet1->setColumn(15,15,10);
		$worksheet1->setColumn(16,16,10);
		$worksheet1->setColumn(17,17,10);
		$worksheet1->setColumn(18,18,10);
		$worksheet1->setColumn(19,19,8);
		$worksheet1->setColumn(20,20,8);
		$worksheet1->setColumn(21,21,8);
		$worksheet1->setColumn(22,22,8);
		$worksheet1->setColumn(23,23,12);
		$worksheet1->setColumn(24,24,9);
		$worksheet1->setColumn(25,25,10);
		$worksheet1->setColumn(26,26,10);
		$worksheet1->setColumn(27,27,10);
		$worksheet1->setColumn(28,28,15);
		$header = &$workbook->addFormat();$header->setBorder(1);$header->setHAlign('center');$header->setBold();$header->setColor('black');$header->setFgColor("grey");//background
		$contentCB = &$workbook->addFormat();$contentCB->setBold();$contentCB->setColor('black');$contentCB->setHAlign('center');
		$contentC = &$workbook->addFormat();$contentC->setColor('black');$contentC->setHAlign('center');
		$contentCI = &$workbook->addFormat();$contentCI->setItalic();$contentCI->setColor('black');$contentCI->setHAlign('center');
		$contentRB = &$workbook->addFormat();$contentRB->setBold();$contentRB->setColor('black');$contentRB->setHAlign('right');$contentRB->setNumFormat('0');
		$contentR = &$workbook->addFormat();$contentR->setColor('black');$contentR->setHAlign('right');
		$contentRI = &$workbook->addFormat();$contentRI->setItalic();$contentRI->setColor('black');$contentRI->setHAlign('right');
		$contentLB = &$workbook->addFormat();$contentLB->setBold();$contentLB->setColor('black');$contentLB->setHAlign('left');
		$contentL = &$workbook->addFormat();$contentL->setColor('black');$contentL->setHAlign('left');
		$contentLI = &$workbook->addFormat();$contentLI->setItalic();$contentLI->setColor('black');$contentLI->setHAlign('left');
		$worksheet1->write(0, 0, '#', $header);
		$worksheet1->write(0, 1, 'ID', $header);
		$worksheet1->write(0, 2, 'Tên công ty', $header);
		$worksheet1->write(0, 3, 'Mã số thuế', $header);
		$worksheet1->write(0, 4, 'Địa chỉ', $header);
		$worksheet1->write(0, 5, 'Người đại diện', $header);
		$worksheet1->write(0, 6, 'Chức vụ', $header);
		$worksheet1->write(0, 7, 'Số hợp đồng', $header);
		$worksheet1->write(0, 8, 'Ngày HĐ', $header);
		$worksheet1->write(0, 9, 'Ngày BB', $header);
		$worksheet1->write(0, 10, 'Tên sản phẩm', $header);
		$worksheet1->write(0, 11, 'Mẫu số', $header);
		$worksheet1->write(0, 12, 'Ký hiệu', $header);
		$worksheet1->write(0, 13, 'ĐV Tính', $header);
		$worksheet1->write(0, 14, 'Số lượng', $header);
		$worksheet1->write(0, 15, 'Đơn giá', $header);
		$worksheet1->write(0, 16, 'Thành tiền', $header);
		$worksheet1->write(0, 17, 'Tiền thuế', $header);
		$worksheet1->write(0, 18, 'Tổng tiền', $header);
		$worksheet1->write(0, 19, 'Số liên', $header);
		$worksheet1->write(0, 20, 'Số màu', $header);
		$worksheet1->write(0, 21, 'Bìa', $header);
		$worksheet1->write(0, 22, 'Ruột', $header);
		$worksheet1->write(0, 23, 'Kích cỡ', $header);
		$worksheet1->write(0, 24, 'Chống ký', $header);
		$worksheet1->write(0, 25, 'Giấy hủy', $header);
		$worksheet1->write(0, 26, 'Bản hủy', $header);
		$worksheet1->write(0, 27, 'Ngày nhập', $header);
		$worksheet1->write(0, 28, 'Người nhập', $header);
		while($rs = mysql_fetch_object($select))
		{
			for($i=1;$i<=$rs->c_product;$i++)
			{
				$pr = mysql_fetch_object(mysql_query("SELECT * FROM product WHERE c_id = '$rs->id' and c_order = '$i' order by c_order asc"));
				if($pr->c_signed == '1'){$c_signed='Có';}else{$c_signed='Không';}
				if($pr->c_unit == 'quyen'){$c_unit='Quyển';$c_quantity1=$pr->c_quantity*50;}elseif($pr->c_unit == 'so'){$c_unit='Số';$c_quantity1=$pr->c_quantity;}else{$c_unit='Thùng';}
				if($pr->c_bowel == 'cacbon'){$c_bowel='Cacbon';}elseif($pr->c_bowel == 'baibang'){$c_bowel='Bãi bằng';}else{$c_bowel='Offset';}
				$us = mysql_fetch_object(mysql_query("SELECT * FROM `user` where `username` = '$rs->c_userlog'"));
				$c_quantity = $c_quantity + $c_quantity1;
				$row = $row + 1;
				$thanhtien = $pr->c_quantity * $pr->c_price;
				$vatamount = $thanhtien * 10/100;
				$tongtien =  $thanhtien + $vatamount;
				$tientruocthue = $tientruocthue + $thanhtien;
				$tienthue = $tienthue + $vatamount;
				$tiensauthue = $tiensauthue + $tongtien;
				$giayhuy = $giayhuy + $pr->c_sohuy;
				$banhuy = $banhuy + $pr->c_banhuy;
				$worksheet1->write($row, 0, $row, $contentCB);
				$worksheet1->write($row, 1, $rs->id, $contentC);
				$worksheet1->write($row, 2, $rs->c_name, $contentLB);
				$worksheet1->write($row, 3, " ".$rs->c_tax, $contentRB);
				$worksheet1->write($row, 4, $rs->c_address, $contentL);
				$worksheet1->write($row, 5, $rs->c_director, $contentL);
				$worksheet1->write($row, 6, $rs->c_position, $contentL);
				$worksheet1->write($row, 7, $rs->c_contract, $contentLB);
				$worksheet1->write($row, 8, date('d/m/Y', $rs->c_date), $contentRB);
				$worksheet1->write($row, 9, $rs->c_datebb, $contentRB);
				$worksheet1->write($row, 10, $pr->c_product_name, $contentLB);
				$worksheet1->write($row, 11, $pr->c_form, $contentRB);
				$worksheet1->write($row, 12, $pr->c_serial, $contentRB);
				$worksheet1->write($row, 13, $c_unit, $contentC);
				$worksheet1->write($row, 14, number_format($pr->c_quantity), $contentR);
				$worksheet1->write($row, 15, number_format($pr->c_price), $contentR);
				$worksheet1->write($row, 16, number_format($thanhtien), $contentR);
				$worksheet1->write($row, 17, number_format($vatamount), $contentR);
				$worksheet1->write($row, 18, number_format($tongtien), $contentRB);
				$worksheet1->write($row, 19, $pr->c_product_page, $contentC);
				$worksheet1->write($row, 20, $pr->c_color, $contentC);
				$worksheet1->write($row, 21, 'Indo', $contentC);
				$worksheet1->write($row, 22, $c_bowel, $contentC);
				$worksheet1->write($row, 23, $pr->c_size, $contentL);
				$worksheet1->write($row, 24, $c_signed, $contentC);
				$worksheet1->write($row, 25, number_format($pr->c_sohuy), $contentC);
				$worksheet1->write($row, 26, $pr->c_banhuy, $contentC);
				$worksheet1->write($row, 27, $rs->c_datelog, $contentR);
				$worksheet1->write($row, 28, $us->fullname, $contentR);
			}
		}
		$worksheet1->write($row+5, 2, 'Tổng số công ty:', $contentL);
		$worksheet1->write($row+6, 2, 'Tổng số hóa đơn:', $contentL);
		$worksheet1->write($row+7, 2, 'Tổng tiền trước thuế:', $contentL);
		$worksheet1->write($row+8, 2, 'Tổng tiền thuế:', $contentL);
		$worksheet1->write($row+9, 2, 'Tổng tiền sau thuế:', $contentL);
		$worksheet1->write($row+10, 2, 'Tổng số giấy hủy:', $contentL);
		$worksheet1->write($row+11, 2, 'Tổng số bản hủy:', $contentL);
		$worksheet1->write($row+5, 3, $tktotalcty, $contentRB);
		$worksheet1->write($row+6, 3, number_format($c_quantity), $contentRB);
		$worksheet1->write($row+7, 3, number_format($tientruocthue), $contentRB);
		$worksheet1->write($row+8, 3, number_format($tienthue), $contentRB);
		$worksheet1->write($row+9, 3, number_format($tiensauthue), $contentRB);
		$worksheet1->write($row+10, 3, number_format($giayhuy), $contentRB);
		$worksheet1->write($row+11, 3, number_format($banhuy), $contentRB);
		$workbook->send($export_file);
		$workbook->close();
	}
}
else
{
	die('File not found!');
}
?>
